import {Provide} from "@midwayjs/decorator";
import {InjectEntityModel} from "@midwayjs/orm";
import {RoleUser} from "../../role/entity/roleUser";
import {In, Not, Repository} from "typeorm";
import * as _ from "lodash";
import {Role} from "../../role/entity/role";


@Provide()
export default class UserRoleService{
  @InjectEntityModel(RoleUser)
  roleUserModel:Repository<RoleUser>;
  @InjectEntityModel(Role)
  roleModel:Repository<Role>

  /*
  * 获取用户已经分配的角色
  * */
  async getUserRoleList(userId:string){
    // @ts-ignore
    let userRoleList=await this.roleUserModel.find({userId: userId});
    let roleIdArr = [];
    _.forEach(userRoleList,function (v) {
      roleIdArr.push(v.roleId);
    })
    // @ts-ignore
    let roleList=await this.roleModel.find({id: In(roleIdArr)})
    return roleList;

  }
  /*
 * 获取用户未分配的角色
 * */
  async getNotUserRoleList(userId:string){
    let roleList=await this.getUserRoleList(userId);  //roleList为用户已经分配的角色
    const roleId: Array<string> = _.map(roleList, "id");
    console.log(roleId);

    let notRoleList = await this.roleModel.find({
      // @ts-ignore
      id: Not(In(roleId))
    });
    return notRoleList;
  }
}
